package Package.Reflection.ReflectionGetsMemberVariablesAndUses;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class ReflectionGetsMemberVariablesAndUses {
    public static void ReflectionGetsMemberVariablesAndUses() throws ClassNotFoundException, NoSuchFieldException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
//        Class类获取成员变量对象的方法
//方法分类
//        方法名 说明
//Field[] getFields() 返回所有公共成员变量对象的数组
        System.out.println("==============================");
        Class<?> c=Class.forName("Package.Example.GetsTheConstructorAndUses.Student");
        Field[] fields=c.getFields();
        for (Field f:fields
             ) {
            System.out.println(f);
        }
        System.out.println("+++++++++++++++++++++++++++++++++++");
        //Field[] getDeclaredFields() 返回所有成员变量对象的数组
        Field[] fields1=c.getDeclaredFields();
        for (Field f:fields1
        ) {
            System.out.println(f);
        }
        System.out.println("+++++++++++++++++++++++++++++++++++++++++");
//Field getField(String name) 返回单个公共成员变量对象
        Field field1=c.getDeclaredField("address");
        System.out.println(field1);
//Field getDeclaredField(String name) 返回单个成员变量对象
        Field field=c.getDeclaredField("name");
        System.out.println(field);
//        voidset(Object obj,Object value) 给obj对象的成员变量赋值为value
        Field addressField = c.getField("address");
        Constructor<?> con = c.getConstructor();
        Object obj = con.newInstance();
        addressField.set(obj,"xian");
        System.out.println(obj);


    }
}
